package sk.stuba.fiit.ltasys.web.mvc.controller;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import sk.stuba.fiit.ltasys.web.mvc.model.entity.AuthoritiesEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.entity.UserEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.service.UserService;


/**
 * User controller.
 * 
 * @author Marek Hruska
 */
@Controller
@SessionAttributes(value = {"userEntity"})
public class UserController {
	
	private static final String LIST_MODEL_KEY  = "list";
	private static final String USER_ENTITY     = "userEntity";
	
	protected static Logger logger = Logger.getLogger("UserController"); 
	
	@Autowired()
	private UserService userService;

	/**
     * <p>Searches for all users and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/user/list'.</p>
     */
    @RequestMapping(value="/user/list", method=RequestMethod.GET)
    public @ModelAttribute(LIST_MODEL_KEY) Collection<UserEntity> getListOfUsers() {
    	logger.debug("Received request to show all users"); 
    	return userService.findUsers();
    }
    
	/**
     * <p>Create new or update old user 
     * <code>UserEntity</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/user/form'.</p>
     */
    @RequestMapping(value="/user/form", method=RequestMethod.GET)
    public @ModelAttribute(USER_ENTITY) UserEntity getUserForm(@RequestParam(value = "username", required = false) String id) {
        if(id==null)
        	return new UserEntity();
        return userService.findUserById(id);
    }
    
	/**
     * <p>Save new or update old user 
     * <code>UserEntity</code>.</p>
     * 
     * <p>Expected HTTP POST and response '/user/saveForm'.</p>
	 * @throws NoSuchAlgorithmException 
     */
    @RequestMapping(value="/user/saveForm", method=RequestMethod.POST)
    public String saveUserForm(UserEntity userEntity, Model model) throws NoSuchAlgorithmException {
    	List<AuthoritiesEntity> authorities = new ArrayList<AuthoritiesEntity>();
    	AuthoritiesEntity authority = new AuthoritiesEntity();
    	authority.setAuthority(AuthoritiesEntity.ROLE_USER);
    	authorities.add(authority);
    	userEntity.setAuthority(authorities);
    	String newPassword = userEntity.calculatePassword(userEntity.getPassword());
    	userEntity.setPassword(newPassword);
    	userEntity.setEnabled(true);
   		userService.save(userEntity);
    	return "redirect:/user/list.html";
    }
    
    /**
     * <p>Deletes a person.</p>
     * 
     * <p>Expected HTTP POST and request '/person/delete'.</p>
     */
    @RequestMapping(value="/user/delete", method=RequestMethod.GET)
    public @ModelAttribute(USER_ENTITY) ModelAndView delete(@RequestParam(value = "username", required = false) String id) {
    	logger.debug("Received request to delete person with id:"+ id);
        userService.delete(userService.findUserById(id));
        
        ModelAndView mv = new ModelAndView();
        mv.addObject(LIST_MODEL_KEY, userService.findUsers());
        mv.setViewName("user/list");
        
        return mv;
        
    }
}
